"""
A class for a Schneeflocke (German for snow flake).
$Id: flocke.py,v 1.6 2004/12/17 09:57:49 krawczyk Exp $
"""

import pygame
import random,config


class Tumbling_Mover(pygame.sprite.Sprite):
    """
    A class for an object which moves down the screen while tumbling.
    It belongs to a sprite group.
    rect -- contains the rectangle which encloses the whole sprite. This is needed for removing the sprite, when it leaves the screen area. It is also needed for the Sprite class so that it can be displayed correctly...
    x -- current x-position
    y -- current y-position
    to_x -- x-position to which the sprite should tumble
    speed -- current speed
    """

    def __init__(self, group):
        "Constructor calls self._Create, which should generate the rectangle self.rect!"
        pygame.sprite.Sprite.__init__(self, group)
        self._Create()
        self._Init()
        self._Update_Rect()

    def _Init(self):
        "Place randomly at the top, sets self.x and self.y"
        fxw, fyw  = self.rect[2:4]
        self.x = random.randint(fxw, config.scrsize[0]-fxw)
        self.to_x = self.x
        self.y = -fyw
        self.speed = random.random()*2+.25

    def _Create(self):
        "Create image and set self.image and self.size"
        raise NotImplementedError

    def _Screen_left(self):
        "This function is called if the sprite leaves the screen."
        raise NotImplementedError

    def _Tumble(self):
        "Funktion responsible for the tumbling of the flake"
        if self.x == self.to_x:
            self.to_x = self.x - random.randint(-12, 12)
        if self.x < self.to_x:
            self.x += 1
        else:
            self.x -= 1

    def _Update_Rect(self):
        "Update the rect to the current position! This function is called at the end of every update!"
        self.rect[0:2] = self.x, self.y

    def update(self):
        self.y += self.speed
        self._Tumble()
        if self.y >= config.scrsize[1] or self.x <= -self.rect[2] or self.x >= config.scrsize[0]:
            self._Screen_left()
        self._Update_Rect()
        

class Flocke(Tumbling_Mover):
    """A class for displaying, etc. of a snow flake."""

    def __init__(self, group):
        Tumbling_Mover.__init__(self, group)
        self.framecount = 0

    def _Create(self):
        self.imglist  = random.choice(flocke_img)
        self.imgcount = 0
        self._SetImg(self.imgcount)

    def _SetImg(self, num):
        self.image = self.imglist[num]
        self.rect = self.image.get_rect()

    def _Screen_left(self):
        self._Create()
        self._Init()
        self._Update_Rect()

    def _Turn(self):
        if self.framecount % 4 == 0:
            self.imgcount += 1
            if self.imgcount >= len(self.imglist):
                self.imgcount = 0
            self._SetImg(self.imgcount)


    def update(self):
        self.framecount += 1
        self._Turn()
        Tumbling_Mover.update(self)



def Init():
    #load the image
    global flocke_img
    flocke_img = []

    config.Status_top("Initialising Flocke...")
    print "Loading snow-flake images:",
    for name in config.schneeflocke:
        print " %s" % name,
        img = pygame.image.load(name).convert_alpha()
        img_l = [img]
        img_l_rev = [img]
        for phi in range(0, 360, 5):
            t = pygame.transform.rotate(img, phi)
            img_l.append(t)
            img_l_rev.append(t)
        img_l_rev.reverse()
        flocke_img.append(img_l)
        flocke_img.append(img_l_rev)
    print
